package com.example.demo.service.impl;

import com.example.demo.dao.TbMessageForBlogDao;
import com.example.demo.entity.TbMessageForBlogEntity;
import com.example.demo.entity.request.Comment;
import com.example.demo.service.TbMessageForBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Optional;

@Service
public class TbMessageForBlogServiceImpl implements TbMessageForBlogService {


    TbMessageForBlogDao messageForBlogDao;
    @Autowired
    public void setMessageForBlogDao(TbMessageForBlogDao messageForBlogDao) {
        this.messageForBlogDao = messageForBlogDao;
    }

    public TbMessageForBlogEntity save(Comment comment) {
        String content = comment.getContent();
        Integer id = comment.getId();
        String nickname = comment.getNickname();
        TbMessageForBlogEntity messageForBlogEntity = new TbMessageForBlogEntity();
        messageForBlogEntity.setBlogId(id);
        messageForBlogEntity.setMessageForBlogAuthor(nickname);
        messageForBlogEntity.setMessageForBlogContent(content);
        messageForBlogEntity.setMessageForBlogDatetime(new Timestamp(System.currentTimeMillis()));
        messageForBlogEntity.setMessageForBlogUp(0);
        return messageForBlogDao.saveAndFlush(messageForBlogEntity);
    }

    @Override
    public void likeMessage(Integer id) {
        Optional<TbMessageForBlogEntity> message = messageForBlogDao.findById(id);
        TbMessageForBlogEntity messageForBlogEntity = message.get();
        int messageForBlogUp = messageForBlogEntity.getMessageForBlogUp();
        messageForBlogUp += 1;
        messageForBlogEntity.setMessageForBlogUp(messageForBlogUp);
        messageForBlogDao.save(messageForBlogEntity);
    }
}
